//
//  LiveController.m
//  JF_New_DYKT
//
//  Created by 陈雷 on 2017/8/22.
//  Copyright © 2017年 陈雷. All rights reserved.
//

#import "LiveController.h"
#import "LiveTuiJianView.h"
#import "LiveZuiXinView.h"
#import "LiveYuYueView.h"
#import "SecondLiveController.h"
#import "SearchLiveController.h"


@interface LiveController ()<UIScrollViewDelegate>

@property (strong, nonatomic) IBOutlet UIView *topView;
@property (nonatomic,strong) UIView *bottomLineV;//按钮底部的线
@property (nonatomic,strong) UIScrollView *baseScrolV;//底部滑动视图
@property (weak, nonatomic) IBOutlet UIButton *tjBtn;

@property (weak, nonatomic) IBOutlet UIButton *zxBtn;

@property (weak, nonatomic) IBOutlet UIButton *yyBtn;

@property (weak, nonatomic) IBOutlet UIView *topBtnV;

@property (nonatomic,strong) LiveTuiJianView *tuiJianTbv;
@property (nonatomic,strong) LiveZuiXinView *zuiXinTbv;
@property (nonatomic,strong) LiveYuYueView *yuYueTbv;


@property (nonatomic,strong) NSArray *btnArr;

@property (nonatomic,strong) UIScrollView *bigSchoolScrolV;



@end

@implementation LiveController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configBaseUI];
}


- (void)configBaseUI{
    
    self.topView.frame = CGRectMake(0, 0, KSCreenWidth, 44);
    
    [self.topBtnV addSubview:self.bottomLineV];
    
    [self.tjBtn setTitleColor:[UIColor colorWithHexString:@"1cda9f"] forState:UIControlStateNormal];
    
    self.bottomLineV.centerX = self.tjBtn.CenterX;
    
    [self.view addSubview:self.baseScrolV];
    
    
    self.schoolScrollV.mj_size = CGSizeMake(KSCreenWidth, 80);
    [self.schoolScrollV addSubview:self.bigSchoolScrolV];
    
    self.tbvHeaderV.frame = CGRectMake(0, 0, KSCreenWidth, 250);
    self.tuiJianTbv.tableHeaderView = self.tbvHeaderV;
    
    [self.baseScrolV addSubview:self.tuiJianTbv];
    [self.baseScrolV addSubview:self.zuiXinTbv];
    [self.baseScrolV addSubview:self.yuYueTbv];
    
    self.btnArr = @[self.tjBtn,self.zxBtn,self.yyBtn];
    
}


#pragma mark- 分类
- (IBAction)classSelectAction:(UIButton *)sender {
    
    [self cancelAllbtnsSelectStatues:sender];
    
    if (sender.tag == 100) {
        NSLog(@"推荐");
    }else if (sender.tag == 200){
        NSLog(@"最新");
    }else if (sender.tag == 300){
        NSLog(@"预约");
    }
    
    [UIView animateWithDuration:.25 animations:^{
        self.bottomLineV.CenterX = sender.CenterX;
        self.baseScrolV.contentOffset = CGPointMake((sender.tag/100 - 1) * KSCreenWidth, 0);
    }];
    
    
}


- (void)cancelAllbtnsSelectStatues:(UIButton *)currentBtn{
    
    for (UIButton *bt in self.btnArr) {
        [bt setTitleColor:[UIColor colorWithHexString:@"070707"] forState:UIControlStateNormal];
    }
    [currentBtn setTitleColor:[UIColor colorWithHexString:@"1cda9f"] forState:UIControlStateNormal];
}

- (UIView *)bottomLineV{
    
    if (!_bottomLineV) {
        _bottomLineV = [[UIView alloc]initWithFrame:CGRectMake(0, 42, 36, 2)];
        _bottomLineV.backgroundColor = [UIColor colorWithHexString:@"1cda9f"];
    }
    return _bottomLineV;
}

- (UIScrollView *)baseScrolV{
    if (!_baseScrolV) {
        _baseScrolV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, KSCreenHeight - 110)];
        _baseScrolV.delegate = self;
        _baseScrolV.contentSize = CGSizeMake(KSCreenWidth*3, KSCreenHeight - 110- 64);
        _baseScrolV.pagingEnabled = YES;
        _baseScrolV.backgroundColor = [UIColor whiteColor];
        _baseScrolV.bounces = NO;
        [_baseScrolV setShowsVerticalScrollIndicator:NO];
        [_baseScrolV setShowsHorizontalScrollIndicator:NO];
    }
    return _baseScrolV;
}

#pragma mark- 滑动下面后上面的也跟着变
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"%lf",scrollView.contentOffset.x);
    
    if (scrollView.contentOffset.x == KSCreenWidth) {
        [self classSelectAction:self.zxBtn];
    }else if (scrollView.contentOffset.x == KSCreenWidth*2){
        [self classSelectAction:self.yyBtn];
    }else{
        [self classSelectAction:self.tjBtn];
    }
    
}

- (LiveTuiJianView *)tuiJianTbv{

    if (!_tuiJianTbv) {
        _tuiJianTbv = [[LiveTuiJianView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, KSCreenHeight - 113) style:UITableViewStylePlain];
    }
    return _tuiJianTbv;
}

- (LiveZuiXinView *)zuiXinTbv{

    if (!_zuiXinTbv) {
        _zuiXinTbv = [[LiveZuiXinView alloc]initWithFrame:CGRectMake(KSCreenWidth, 0, KSCreenWidth, KSCreenHeight - 113) style:UITableViewStylePlain];
    }
    return _zuiXinTbv;
}

- (LiveYuYueView *)yuYueTbv{
    if (!_yuYueTbv) {
        _yuYueTbv = [[LiveYuYueView alloc]initWithFrame:CGRectMake(2*KSCreenWidth, 0, KSCreenWidth, KSCreenHeight - 113) style:UITableViewStylePlain];
    }
    return _yuYueTbv;
}


- (IBAction)searchAction:(UIButton *)sender {
    
    NSLog(@"搜索🔍");
    SearchLiveController *searchVc = [[SearchLiveController alloc]init];
    [searchVc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:searchVc animated:YES];
}

- (UIScrollView *)bigSchoolScrolV{
    if (!_bigSchoolScrolV) {
        _bigSchoolScrolV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, 80)];
        
        int num = 5;
        _bigSchoolScrolV.contentSize = CGSizeMake(90*num + 20, 80);
        
        _bigSchoolScrolV.showsHorizontalScrollIndicator = NO;
        
        [self addBtnsToView:_bigSchoolScrolV btnsCount:num];
    }
    return _bigSchoolScrolV;
}

- (void)addBtnsToView:(UIScrollView *)scrollV btnsCount:(int)btnNum{
    
    for (int i = 0; i < btnNum; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor colorWithRed:arc4random_uniform(200)/255.0 green:arc4random_uniform(200)/255.0 blue:arc4random_uniform(200)/255.0 alpha:1];
        
        btn.frame = CGRectMake(90*i + 20, 10, 70, 60);
        [self.bigSchoolScrolV addSubview:btn];
        
    }
}

- (void)btnAction{
    SecondLiveController *secondLiveVc = [[SecondLiveController alloc]init];
    [secondLiveVc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:secondLiveVc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:self.topView];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.topView removeFromSuperview];
}

#pragma mark- 开始直播

- (IBAction)startLiveAction:(UIButton *)sender {
    
    NSLog(@"开始直播");
    
}

@end
